const {
  DataTypes
} = require('sequelize');

const DateUtils = require('../utils/dateUtils');
module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "主键 ",
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    dictCode: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "字典编码",
      primaryKey: false,
      field: "dict_code",
      autoIncrement: false
    },
    name: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "类型编码",
      primaryKey: false,
      field: "name",
      autoIncrement: false
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "状态(0：禁用，1：正常)",
      primaryKey: false,
      field: "status",
      autoIncrement: false
    },
    remark: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "备注",
      primaryKey: false,
      field: "remark",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('updateTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    updateTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "更新时间",
      primaryKey: false,
      field: "update_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('updateTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    isDeleted: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "是否删除(0：未删除，1：已删除)",
      primaryKey: false,
      field: "is_deleted",
      autoIncrement: false
    }
  };
  const options = {
    tableName: "sys_dict",
    comment: "",
    indexes: []
  };
  const SysDictModel = sequelize.define("sys_dict_model", attributes, options);

  SysDictModel.associate = function(models) {
    // 定义与sys_dict_data的一对多关联
    SysDictModel.hasMany(models.sys_dict_data_model, {
      foreignKey: 'dictCode',
      sourceKey: 'dictCode'
    });
  };

  return SysDictModel;
};